package com.citybox.shared.web.domain;

import com.citybox.shared.exception.Exp;
import com.citybox.shared.exception.ICode;
import com.citybox.shared.exception.enums.RCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 操作消息提醒
 **/
@Data
public class R<T> implements Serializable {

    /**
     * 是否成功，默认为 true
     */
    private boolean success = true;
    /**
     * 响应消息
     */
    private String msg;
    /**
     * 异常码
     */
    private int code;
    /**
     * 响应数据
     */
    private T data;

    public static <T> R<T> success() {
        return new R<>();
    }

    public static <T> R<T> ok(T data) {
        R<T> response = new R<>();
        response.setSuccess(true);
        response.setMsg(RCode.success.getMessage());
        response.setCode(RCode.success.getCode());
        response.setData(data);
        return response;
    }

    public static <T> R<T> success(T data,String msg) {
        R<T> response = new R<>();
        response.setSuccess(true);
        response.setMsg(msg);
        response.setCode(RCode.success.getCode());
        response.setData(data);
        return response;
    }

    public static <T> R<T> fail() {
        R<T> response = new R<>();
        response.setSuccess(false);
        return response;
    }

    public static <T> R<T> fail(String errorMessage) {
        R<T> response = new R<>();
        response.setSuccess(false);
        response.setMsg(errorMessage);
        return response;
    }

    public static <T> R<T> fail(int errorCode, String errorMessage) {
        R<T> response = new R<>();
        response.setSuccess(false);
        response.setCode(errorCode);
        response.setMsg(errorMessage);
        return response;
    }

    public static <T> R<T> fail(Exp exp) {
        R<T> response = new R<>();
        response.setSuccess(false);
        response.setCode(exp.getCode());
        response.setMsg(exp.getMessage());
        return response;
    }

    public static <T> R<T> fail(ICode ICode) {
        R<T> response = new R<>();
        response.setSuccess(false);
        response.setCode(ICode.getCode());
        response.setMsg(ICode.getMessage());
        return response;
    }
}